package com.youdu.vuandroidadsdk.imageloader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.youdu.vuandroidadsdk.R;

/**
 * @Title:ImageLoaderManager
 * @Package:com.youdu.vuandroidadsdk.imageloader
 * @Description: 初始化 UniverImageLoader,并用来加载图片；
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/7/2315:54
 */
public class ImageLoaderManager {
    private static final int THREAD_COUNT = 4;//线程数量
    private static final int PROPRITY = 2;//图片加载的优先级
    private static final int DISK_CACHE_SIZE = 50*1024;//本地缓存空间大小
    private static final int CONNECT_TIME_OUT = 5*1000;//连接超时
    private static final int READ_TIME_OUT = 30*1000;//读取超时


    private static ImageLoader mImageLoader;
    private static ImageLoaderManager instance;

    private ImageLoaderManager(Context cxt) {
        ImageLoaderConfiguration ic = new ImageLoaderConfiguration.Builder(cxt)

                .threadPoolSize(THREAD_COUNT)
                .threadPriority(Thread.NORM_PRIORITY - PROPRITY)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new WeakMemoryCache())
                .diskCacheSize(DISK_CACHE_SIZE)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)//图片下载顺序
                .defaultDisplayImageOptions(getDefaultOptions())
                .imageDownloader(new BaseImageDownloader(cxt, CONNECT_TIME_OUT, READ_TIME_OUT))//设置图片的下载器
                .writeDebugLogs()//debug模式下会输出日志
                .build();


        ImageLoader.getInstance().init(ic);
        mImageLoader = ImageLoader.getInstance();
    }

    //单例获取实例
    public static ImageLoaderManager getInstance(Context cxt){
        if(null == instance){
            synchronized (ImageLoaderManager.class){
                if(null == instance){
                    instance = new ImageLoaderManager(cxt);
                }
            }
        }

        return instance;
    }

    private DisplayImageOptions getDefaultOptions() {

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_error_white_36dp)
                .showImageOnFail(R.drawable.instabug_img_placeholder)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)//使用图片的解码类型
                .decodingOptions(new BitmapFactory.Options())//图片解码方式
                .build();
        return defaultOptions;
    }


    /**
     * 功能：展示图片API
     * @param imgView
     * @param url
     * @param options
     * @param listener
     */
    public void disPlayImage(ImageView imgView,
                             String url ,
                             DisplayImageOptions options,
                             ImageLoadingListener listener){


        if(null != mImageLoader){
            mImageLoader.displayImage(url,imgView,options,listener);
        }
    }



    public void disPlayImage(ImageView imgView,
                             String url, ImageLoadingListener listener){
        disPlayImage(imgView, url, getDefaultOptions(), listener);
    }


    public void disPlayImage(ImageView imgView,
                             String url){
        disPlayImage(imgView, url, getDefaultOptions(), null);
    }
}
